/**
  ******************************************************************************
  * @file    ACAD_MEASURE.h
  * @author  Sherlock
  * @version V0.1
  * @date    10/12/2023
  * @brief   Outputs Driver
  ******************************************************************************
  * @copy
  *
  * <h2><center>&copy; COPYRIGHT 2010 Feirui</center></h2>
  */ 



/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef  __ACAD_MEASURE_H__
#define  __ACAD_MEASURE_H__


#ifdef __cplusplus
 extern "C" {
#endif


/**
  * @brief  INCLUDE FILES.
  */
#include "acad_core.h"


/**
  * @brief GLOBALS RAM definition
  */
#ifdef   ACAD_MEASURE_GLOBALS
#define  ACAD_MEASURE_EXT
#else
#define  ACAD_MEASURE_EXT  extern
#endif


/*******************************************************************************************************
 *                                          测量参数
 *******************************************************************************************************/
/* 硬件校准系数 */
#define DefaultValHWCalibGenVoltP1               ((float)0.2637)  //相电压
#define DefaultValHWCalibGenVoltP2               ((float)0.2637)
#define DefaultValHWCalibGenVoltP3               ((float)0.2637)

#define DefaultValHWCalibGenVoltL12              ((float)1.0017) //线电压
#define DefaultValHWCalibGenVoltL23              ((float)1.0017)
#define DefaultValHWCalibGenVoltL31              ((float)1.0017)

#define DefaultValHWCalibGenCurrL1               ((float)0.00540897) //电流
#define DefaultValHWCalibGenCurrL2               ((float)0.00540897)
#define DefaultValHWCalibGenCurrL3               ((float)0.00540897)
// #define DefaultValHWCalibGenCurrL1               ((float)0.0094) //电流
// #define DefaultValHWCalibGenCurrL2               ((float)0.0094)
// #define DefaultValHWCalibGenCurrL3               ((float)0.0094)

#define DefaultValHWCalibGenKWL1                 ((float)1) //有功功率
#define DefaultValHWCalibGenKWL2                 ((float)1)
#define DefaultValHWCalibGenKWL3                 ((float)1)

#define DefaultValHWCalibGenKVArL1               ((float)1)  //无功功率
#define DefaultValHWCalibGenKVArL2               ((float)1)
#define DefaultValHWCalibGenKVArL3               ((float)1)

// #define DefaultValHWCalibGenExciteVolt           ((float)0.006726)   //励磁
// #define DefaultValHWCalibGenExciteVolt           ((float)0.04143)   //励磁
#define DefaultValHWCalibGenExciteVolt           ((float)0.0687)   //励磁
#define DefaultValHWCalibGenExciteCurr           ((float)0.0040288)

#define DefaultValHWCalibBuildVoltBEF            ((float)0.05112)  //建磁电压（前）
#define DefaultValHWCalibBuildVoltAFT            ((float)0.05112)  //建磁电压（后）

/* 软件校准索引 */
#define SWCalibCoeffi_GenPhaseVoltL1          0    //被测电机相电压
#define SWCalibCoeffi_GenPhaseVoltL2          1
#define SWCalibCoeffi_GenPhaseVoltL3          2
#define SWCalibCoeffi_GenLineVoltL12          3    //被测电机线电压
#define SWCalibCoeffi_GenLineVoltL23          4
#define SWCalibCoeffi_GenLineVoltL31          5
#define SWCalibCoeffi_LoadPhaseVoltL1         6    //负载电机相电压
#define SWCalibCoeffi_LoadPhaseVoltL2         7
#define SWCalibCoeffi_LoadPhaseVoltL3         8
#define SWCalibCoeffi_LoadLineVoltL12         9    //负载电机线电压
#define SWCalibCoeffi_LoadLineVoltL23         10
#define SWCalibCoeffi_LoadLineVoltL31         11
// #define SWCalibCoeffi_GenExciteVoltPassZero   15
#define SWCalibCoeffi_GenExciteVolt           12   //被测电机励磁
// #define SWCalibCoeffi_GenExciteCurrPassZero   17
#define SWCalibCoeffi_GenExciteCurr           13
// #define SWCalibCoeffi_LoadExciteVoltassZero   19
#define SWCalibCoeffi_LoadExciteVolt          14   //负载电机励磁
// #define SWCalibCoeffi_LoadExciteCurrassZero   21
#define SWCalibCoeffi_LoadExciteCurr          15
#define SWCalibCoeffi_GenPhaseCurrL1          16   //被测电机相电流
#define SWCalibCoeffi_GenPhaseCurrL2          17    
#define SWCalibCoeffi_GenPhaseCurrL3          18   
#define SWCalibCoeffi_BuildVoltBEF            19   //建磁电压
#define SWCalibCoeffi_BuildVoltAFT            20

#define SWCalibCoeffi_TOTNum                  21

/* 变压器互感器比率索引 */
#define HWTransformRatio_PrimaryVolt          0
#define HWTransformRatio_SecondaryVolt        1
#define HWTransformRatio_PrimaryCurr          2
#define HWTransformRatio_SecondaryCurr        3
#define HWTransformRatio_TOTNum               (4*2)

/* 滤波滞后偏移量 */
#define ACLowPassFilterDelay          0

/*******************************************************************************************************
 *                                          滤波系数
 *******************************************************************************************************/
#define LowPassFilterValue_GenExciteVolt        ((float)0.001)
#define LowPassFilterValue_GenExciteCurr        ((float)0.01)
#define LowPassFilterValue_GenPower             ((float)0.05)


/*******************************************************************************************************
 *                                         ACAD测量变量
 *******************************************************************************************************/
/* 过零点结构体 */
ACAD_MEASURE_EXT PassPointMsgType PassPointMsgGensU;  //被测电机
ACAD_MEASURE_EXT PassPointMsgType PassPointMsgGensV;
ACAD_MEASURE_EXT PassPointMsgType PassPointMsgGensW;
// ACAD_MEASURE_EXT PassPointMsgType PassPointMsgCurrU;  //被测电机电流
// ACAD_MEASURE_EXT PassPointMsgType PassPointMsgCurrV;
// ACAD_MEASURE_EXT PassPointMsgType PassPointMsgCurrW;
ACAD_MEASURE_EXT PassPointMsgType PassPointMsgLoadU;  //负载电机
ACAD_MEASURE_EXT PassPointMsgType PassPointMsgLoadV;
ACAD_MEASURE_EXT PassPointMsgType PassPointMsgLoadW;
ACAD_MEASURE_EXT PassPointMsgType PassPointMsgBuildVoltBEF;

/* 弱信号检测参数 */
ACAD_MEASURE_EXT int16_t RecordGensUTraceMax;  //被测电机
ACAD_MEASURE_EXT int16_t RecordGensVTraceMax;
ACAD_MEASURE_EXT int16_t RecordGensWTraceMax;
// ACAD_MEASURE_EXT int16_t RecordCurrUTraceMax;  //被测电机电流
// ACAD_MEASURE_EXT int16_t RecordCurrVTraceMax;
// ACAD_MEASURE_EXT int16_t RecordCurrWTraceMax;
ACAD_MEASURE_EXT int16_t RecordLoadUTraceMax;  //负载电机
ACAD_MEASURE_EXT int16_t RecordLoadVTraceMax;
ACAD_MEASURE_EXT int16_t RecordLoadWTraceMax;
ACAD_MEASURE_EXT int16_t RecordBuildVoltBEFTraceMax;  //建磁电压

ACAD_MEASURE_EXT int16_t RecordGensUTraceMin;  //被测电机
ACAD_MEASURE_EXT int16_t RecordGensVTraceMin;
ACAD_MEASURE_EXT int16_t RecordGensWTraceMin;
// ACAD_MEASURE_EXT int16_t RecordCurrUTraceMin;  //被测电机电流
// ACAD_MEASURE_EXT int16_t RecordCurrVTraceMin;
// ACAD_MEASURE_EXT int16_t RecordCurrWTraceMin;
ACAD_MEASURE_EXT int16_t RecordLoadUTraceMin;  //负载电机
ACAD_MEASURE_EXT int16_t RecordLoadVTraceMin;
ACAD_MEASURE_EXT int16_t RecordLoadWTraceMin;
ACAD_MEASURE_EXT int16_t RecordBuildVoltBEFTraceMin;  //建磁电压

ACAD_MEASURE_EXT int16_t GensUTraceDCLimitUpper;  //被测电机
ACAD_MEASURE_EXT int16_t GensVTraceDCLimitUpper;
ACAD_MEASURE_EXT int16_t GensWTraceDCLimitUpper;
// ACAD_MEASURE_EXT int16_t CurrUTraceDCLimitUpper;  //被测电机电流
// ACAD_MEASURE_EXT int16_t CurrVTraceDCLimitUpper;
// ACAD_MEASURE_EXT int16_t CurrWTraceDCLimitUpper;
ACAD_MEASURE_EXT int16_t LoadUTraceDCLimitUpper;  //负载电机
ACAD_MEASURE_EXT int16_t LoadVTraceDCLimitUpper;
ACAD_MEASURE_EXT int16_t LoadWTraceDCLimitUpper;
ACAD_MEASURE_EXT int16_t BuildVoltBEFTraceDCLimitUpper;  //建磁电压

ACAD_MEASURE_EXT int16_t GensUTraceDCLimitLower;  //被测电机
ACAD_MEASURE_EXT int16_t GensVTraceDCLimitLower;
ACAD_MEASURE_EXT int16_t GensWTraceDCLimitLower;
// ACAD_MEASURE_EXT int16_t CurrUTraceDCLimitLower;  //被测电机电流
// ACAD_MEASURE_EXT int16_t CurrVTraceDCLimitLower;
// ACAD_MEASURE_EXT int16_t CurrWTraceDCLimitLower;
ACAD_MEASURE_EXT int16_t LoadUTraceDCLimitLower;  //负载电机
ACAD_MEASURE_EXT int16_t LoadVTraceDCLimitLower;
ACAD_MEASURE_EXT int16_t LoadWTraceDCLimitLower;
ACAD_MEASURE_EXT int16_t BuildVoltBEFTraceDCLimitLower;  //建磁电压

/* 过零点检测参数 */
ACAD_MEASURE_EXT uint8_t GensUTraceRecordNum;  //被测电机
ACAD_MEASURE_EXT uint8_t GensUTraceRecordSta;
ACAD_MEASURE_EXT uint8_t GensUTraceP0Counter;
ACAD_MEASURE_EXT uint8_t GensUTraceLVCounter;
ACAD_MEASURE_EXT uint8_t GensUTraceLVStatus ;
// ACAD_MEASURE_EXT uint8_t CurrUTraceRecordNum;  //被测电机电流
// ACAD_MEASURE_EXT uint8_t CurrUTraceRecordSta;
// ACAD_MEASURE_EXT uint8_t CurrUTraceP0Counter;
// ACAD_MEASURE_EXT uint8_t CurrUTraceLVCounter;
// ACAD_MEASURE_EXT uint8_t CurrUTraceLVStatus ;
ACAD_MEASURE_EXT uint8_t LoadUTraceRecordNum;  //负载电机
ACAD_MEASURE_EXT uint8_t LoadUTraceRecordSta;
ACAD_MEASURE_EXT uint8_t LoadUTraceP0Counter;
ACAD_MEASURE_EXT uint8_t LoadUTraceLVCounter;
ACAD_MEASURE_EXT uint8_t LoadUTraceLVStatus ;

ACAD_MEASURE_EXT uint8_t GensVTraceRecordNum;  //被测电机
ACAD_MEASURE_EXT uint8_t GensVTraceRecordSta;
ACAD_MEASURE_EXT uint8_t GensVTraceP0Counter;
ACAD_MEASURE_EXT uint8_t GensVTraceLVCounter;
ACAD_MEASURE_EXT uint8_t GensVTraceLVStatus ;
// ACAD_MEASURE_EXT uint8_t CurrVTraceRecordNum;  //被测电机电流
// ACAD_MEASURE_EXT uint8_t CurrVTraceRecordSta;
// ACAD_MEASURE_EXT uint8_t CurrVTraceP0Counter;
// ACAD_MEASURE_EXT uint8_t CurrVTraceLVCounter;
// ACAD_MEASURE_EXT uint8_t CurrVTraceLVStatus ;
ACAD_MEASURE_EXT uint8_t LoadVTraceRecordNum;  //负载电机
ACAD_MEASURE_EXT uint8_t LoadVTraceRecordSta;
ACAD_MEASURE_EXT uint8_t LoadVTraceP0Counter;
ACAD_MEASURE_EXT uint8_t LoadVTraceLVCounter;
ACAD_MEASURE_EXT uint8_t LoadVTraceLVStatus ;

ACAD_MEASURE_EXT uint8_t GensWTraceRecordNum;  //被测电机
ACAD_MEASURE_EXT uint8_t GensWTraceRecordSta;
ACAD_MEASURE_EXT uint8_t GensWTraceP0Counter;
ACAD_MEASURE_EXT uint8_t GensWTraceLVCounter;
ACAD_MEASURE_EXT uint8_t GensWTraceLVStatus ;
// ACAD_MEASURE_EXT uint8_t CurrWTraceRecordNum;  //被测电机电流
// ACAD_MEASURE_EXT uint8_t CurrWTraceRecordSta;
// ACAD_MEASURE_EXT uint8_t CurrWTraceP0Counter;
// ACAD_MEASURE_EXT uint8_t CurrWTraceLVCounter;
// ACAD_MEASURE_EXT uint8_t CurrWTraceLVStatus ;
ACAD_MEASURE_EXT uint8_t LoadWTraceRecordNum;  //负载电机
ACAD_MEASURE_EXT uint8_t LoadWTraceRecordSta;
ACAD_MEASURE_EXT uint8_t LoadWTraceP0Counter;
ACAD_MEASURE_EXT uint8_t LoadWTraceLVCounter;
ACAD_MEASURE_EXT uint8_t LoadWTraceLVStatus ;

ACAD_MEASURE_EXT uint8_t BuildVoltBEFTraceRecordNum;  //建磁电压
ACAD_MEASURE_EXT uint8_t BuildVoltBEFTraceRecordSta;
ACAD_MEASURE_EXT uint8_t BuildVoltBEFTraceP0Counter;
ACAD_MEASURE_EXT uint8_t BuildVoltBEFTraceLVCounter;
ACAD_MEASURE_EXT uint8_t BuildVoltBEFTraceLVStatus ;

ACAD_MEASURE_EXT uint8_t ACTrace1stRecordFlag;   //首次检测标志

/* ACAD全局频率，用于当前无过零点时，复用全局频率 */
ACAD_MEASURE_EXT float GensUGlobalVoltFre;
ACAD_MEASURE_EXT float GensVGlobalVoltFre;
ACAD_MEASURE_EXT float GensWGlobalVoltFre;
// ACAD_MEASURE_EXT float CurrUGlobalVoltFre;
// ACAD_MEASURE_EXT float CurrVGlobalVoltFre;
// ACAD_MEASURE_EXT float CurrWGlobalVoltFre;
ACAD_MEASURE_EXT float LoadUGlobalVoltFre;
ACAD_MEASURE_EXT float LoadVGlobalVoltFre;
ACAD_MEASURE_EXT float LoadWGlobalVoltFre;
ACAD_MEASURE_EXT float BuildVoltBEFGlobalVoltFre;

/* 切比雪夫滤波器 */
#define ACFilterNL    3
ACAD_MEASURE_EXT int64_t ChebyGensVoltV1LowpassXn[ACFilterNL];
ACAD_MEASURE_EXT int64_t ChebyGensVoltV2LowpassXn[ACFilterNL];
ACAD_MEASURE_EXT int64_t ChebyGensVoltV3LowpassXn[ACFilterNL];
ACAD_MEASURE_EXT int64_t ChebyGensVoltV1LowpassYn[ACFilterNL];
ACAD_MEASURE_EXT int64_t ChebyGensVoltV2LowpassYn[ACFilterNL];
ACAD_MEASURE_EXT int64_t ChebyGensVoltV3LowpassYn[ACFilterNL];

// ACAD_MEASURE_EXT int64_t ChebyGensCurrV1LowpassXn[ACFilterNL];
// ACAD_MEASURE_EXT int64_t ChebyGensCurrV2LowpassXn[ACFilterNL];
// ACAD_MEASURE_EXT int64_t ChebyGensCurrV3LowpassXn[ACFilterNL];
// ACAD_MEASURE_EXT int64_t ChebyGensCurrV1LowpassYn[ACFilterNL];
// ACAD_MEASURE_EXT int64_t ChebyGensCurrV2LowpassYn[ACFilterNL];
// ACAD_MEASURE_EXT int64_t ChebyGensCurrV3LowpassYn[ACFilterNL];

ACAD_MEASURE_EXT int64_t ChebyLoadVoltV1LowpassXn[ACFilterNL];
ACAD_MEASURE_EXT int64_t ChebyLoadVoltV2LowpassXn[ACFilterNL];
ACAD_MEASURE_EXT int64_t ChebyLoadVoltV3LowpassXn[ACFilterNL];
ACAD_MEASURE_EXT int64_t ChebyLoadVoltV1LowpassYn[ACFilterNL];
ACAD_MEASURE_EXT int64_t ChebyLoadVoltV2LowpassYn[ACFilterNL];
ACAD_MEASURE_EXT int64_t ChebyLoadVoltV3LowpassYn[ACFilterNL];

ACAD_MEASURE_EXT int64_t ChebyBuildVoltBEFLowpassXn[ACFilterNL];  //建磁电压
ACAD_MEASURE_EXT int64_t ChebyBuildVoltBEFLowpassYn[ACFilterNL];

//励磁历史累积值
ACAD_MEASURE_EXT float ADCLastValue_GenExciteVolt;   //被测电机
ACAD_MEASURE_EXT float ADCLastValue_GenExciteCurr;
ACAD_MEASURE_EXT float ADCLastValue_LoadExciteVolt;  //负载电机
ACAD_MEASURE_EXT float ADCLastValue_LoadExciteCurr;

//同步误差初次测量标志
ACAD_MEASURE_EXT uint8_t SyncErrU1stSta;
ACAD_MEASURE_EXT uint8_t SyncErrV1stSta;
ACAD_MEASURE_EXT uint8_t SyncErrW1stSta;
ACAD_MEASURE_EXT int8_t SyncErrModeLevelU;
ACAD_MEASURE_EXT int8_t SyncErrModeLevelV;
ACAD_MEASURE_EXT int8_t SyncErrModeLevelW;
ACAD_MEASURE_EXT float SyncLastErrU;
ACAD_MEASURE_EXT float SyncLastErrV;
ACAD_MEASURE_EXT float SyncLastErrW;

/* 功率数据中值滤波 */
// ACAD_MEASURE_EXT MediumFilterObjTypeDef MemGenPowerP1;
// ACAD_MEASURE_EXT MediumFilterObjTypeDef MemGenPowerP2;
// ACAD_MEASURE_EXT MediumFilterObjTypeDef MemGenPowerP3;
// ACAD_MEASURE_EXT MediumFilterObjTypeDef MemGenPowerQ1;
// ACAD_MEASURE_EXT MediumFilterObjTypeDef MemGenPowerQ2;
// ACAD_MEASURE_EXT MediumFilterObjTypeDef MemGenPowerQ3;
// ACAD_MEASURE_EXT MediumFilterObjTypeDef MemGenPowerS1;
// ACAD_MEASURE_EXT MediumFilterObjTypeDef MemGenPowerS2;
// ACAD_MEASURE_EXT MediumFilterObjTypeDef MemGenPowerS3;

//滤波记忆数据，采用一阶惯性或者中值滤波方式
ACAD_MEASURE_EXT float MemGenPowerP1;   //功率数据
ACAD_MEASURE_EXT float MemGenPowerP2;
ACAD_MEASURE_EXT float MemGenPowerP3;
ACAD_MEASURE_EXT float MemGenPowerQ1;
ACAD_MEASURE_EXT float MemGenPowerQ2;
ACAD_MEASURE_EXT float MemGenPowerQ3;
ACAD_MEASURE_EXT float MemGenPowerS1;
ACAD_MEASURE_EXT float MemGenPowerS2;
ACAD_MEASURE_EXT float MemGenPowerS3;
ACAD_MEASURE_EXT float MemGenPFL1;
ACAD_MEASURE_EXT float MemGenPFL2;
ACAD_MEASURE_EXT float MemGenPFL3;

/*******************************************************************************************************
 *                                         Function
 *******************************************************************************************************/
uint8_t ACAD_MeasureHandle(void);
void FillGeneratorDataIntoRTMemory(ACAD_GeneratorDataTypeDef *GenSetData,float phaseVoltL1,float phaseVoltL2,float phaseVoltL3,
	float lineVoltL12,float lineVoltL23,float lineVoltL31,float freqL12,float freqL23,float freqL31,float phaseDiffU2U1,float phaseDiffU3U1);
void FillExcitationDataIntoRTMemory(ACAD_ExcitationDataTypeDef *ExcitationData,float excitationVolt,float excitationCurr);
void FillSynchronousDataIntoRTMemory(ACAD_SynchronousDataTypeDef *SyncData,float voltDiffU1,float voltDiffU2,float voltDiffU3,
	float phaseDiffU1,float phaseDiffU2,float phaseDiffU3,float syncErrU1,float syncErrU2,float syncErrU3);
void FillPowerDataIntoRTMemory(ACAD_PowerDataTypeDef *PowerData,float currL1,float currL2,float currL3,float powerP1,float powerP2,
	float powerP3,float powerQ1,float powerQ2,float powerQ3,float powerS1,float powerS2,float powerS3,float PFL1,float PFL2,float PFL3);
void FillBuildVoltDataIntoRTMemory(ACAD_BuildVoltDataTypeDef *BuildVoltData,float buildVoltFreq,float BuildVoltBEF,float BuildVoltAFT);


#ifdef __cplusplus
}
#endif



#endif   /*  __ACAD_MEASURE_H__  */

/******************* (C) COPYRIGHT 2010 Feirui *****END OF FILE****/